package com.smartscreen.contentmanage.controller;

import com.smartscreen.contentmanage.entity.Message;
import com.smartscreen.contentmanage.service.MessageService;
import com.smartscreen.common.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/content/message-boards")
@Validated
public class MessageBoardController {
    
    @Autowired
    private MessageService messageService;
    
    /**
     * 创建留言
     * @param boardId 留言板ID
     * @param message 留言内容
     * @return 创建结果
     */
    @PostMapping("/{boardId}/messages")
    public ApiResponse<Map<String, String>> createMessage(
            @PathVariable String boardId,
            @RequestBody Message message) {
        
        try {
            if (boardId == null || boardId.trim().isEmpty()) {
                return ApiResponse.badRequest("留言板ID不能为空");
            }
            
            if (message == null) {
                return ApiResponse.badRequest("留言内容不能为空");
            }
            
            message.setBoardId(boardId);
            message.setStatus("pending"); // 默认待审核
            boolean success = messageService.save(message);
            
            if (success) {
                Map<String, String> data = new HashMap<>();
                data.put("messageId", message.getMessageId());
                return ApiResponse.created("留言成功，等待审核", data);
            } else {
                return ApiResponse.serverError("留言失败");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("留言失败：" + e.getMessage());
        }
    }
    
    /**
     * 回复留言
     * @param messageId 原留言ID
     * @param reply 回复内容
     * @return 回复结果
     */
    @PostMapping("/messages/{messageId}/replies")
    public ApiResponse<Map<String, String>> replyMessage(
            @PathVariable String messageId,
            @RequestBody Message reply) {
        
        try {
            if (messageId == null || messageId.trim().isEmpty()) {
                return ApiResponse.badRequest("留言ID不能为空");
            }
            
            if (reply == null) {
                return ApiResponse.badRequest("回复内容不能为空");
            }
            
            reply.setParentId(messageId);
            reply.setStatus("approved"); // 回复默认通过
            boolean success = messageService.save(reply);
            
            if (success) {
                Map<String, String> data = new HashMap<>();
                data.put("replyId", reply.getMessageId());
                return ApiResponse.created("回复成功", data);
            } else {
                return ApiResponse.serverError("回复失败");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("回复失败：" + e.getMessage());
        }
    }
    
    /**
     * 审核留言
     * @param messageId 留言ID
     * @param request 审核请求
     * @return 审核结果
     */
    @PostMapping("/messages/{messageId}/approval")
    public ApiResponse<Void> approveMessage(
            @PathVariable String messageId,
            @RequestBody Map<String, String> request) {
        
        try {
            if (messageId == null || messageId.trim().isEmpty()) {
                return ApiResponse.badRequest("留言ID不能为空");
            }
            
            String status = request.get("status");
            if (status == null || status.trim().isEmpty()) {
                return ApiResponse.badRequest("审核状态不能为空");
            }
            
            boolean success = messageService.approveMessage(messageId, status);
            
            if (success) {
                return ApiResponse.success("审核操作成功");
            } else {
                return ApiResponse.serverError("审核操作失败");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("审核操作失败：" + e.getMessage());
        }
    }
}